package cn.life.business.car.controller;

import cn.life.business.car.entity.po.Car;
import cn.life.business.car.entity.vo.CarVo;
import cn.life.business.car.service.CarService;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.model.MyUser;
import cn.life.common.entity.model.MyUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description:    工厂汽车加油控制层
 * @author wangzhenyu
 * @Date 2018/7/6  16:45
 */
@RestController
@RequestMapping("car")
public class CarController {

    @Autowired private CarService carService;

    /**
     * @Description:  添加汽车加油记录
     * @param   car
     * @return
     */
    @PostMapping(value = "/addcar")
    public CMessage addCar(@RequestBody Car car, @MyUser MyUserInfo myUserInfo){
        getOpr(car,myUserInfo);
        return carService.addCar(car);
    }

    /**
     * @Description:  删除汽车加油记录
     * @param   car
     * @return
     */
    @PostMapping(value = "/deletecar")
    public CMessage deletecar(@RequestBody Car car){
        return carService.deleteCar(car.getCarId());
    }

    /**
     * @Description:  修改汽车加油记录
     * @param   car
     * @return
     */
    @PostMapping(value = "/updatecar")
    public CMessage updatecar(@RequestBody Car car){
        return carService.updateCar(car);
    }

    /**
     * @Description:  查询汽车加油记录
     * @param   carVo
     * @return
     */
    @PostMapping(value = "getcarinfo")
    public CMessage getAllCarInfo(@RequestBody CarVo carVo,@MyUser MyUserInfo myUserInfo) throws ParseException {
        if(carVo.getProjectId() == null){
            carVo.setProjectId(myUserInfo.getProjectId());
        }
        return  carService.getAllCarInfo(carVo);
    }

    /**
     * @Description:  查询单条（详情页面）汽车加油记录
     * @param   carVo
     * @return
     */
    @PostMapping(value = "getcar")
    public CMessage getCar(@RequestBody CarVo carVo){
       return carService.getCar(carVo);
    }
    /**
     * @Description:  自动添加项
     * @param   car,myUserInfo
     * @return
     */
    public Car getOpr(Car car, MyUserInfo myUserInfo){
        if(car.getProjectId() == null){
            car.setProjectId(myUserInfo.getProjectId());
        }
        car.setOprUserId(myUserInfo.getUserId());
        car.setOprUserName(myUserInfo.getUserName());
        car.setOprDate(new Date());
        return car;
    }
}
